<?php

namespace app\index\controller;

use app\common\CommonController;
use app\common\model\ToolModel;
use app\index\model\UserModel;
use app\index\service\UserService;

class User extends CommonController
{
    public function __construct()
    {
        parent::__construct();
    }
    //用户列表
    public function getList()
    {
        exitJson(UserService::getList($this->param));
    }
    //登陆
    public function login()
    {
        $rules = [
            'username' => 'require',
            'pass' => 'require',
        ];
        $this->validateData($rules);
        $ret = UserService::ssoLogin(trim($this->request->param('username')), $this->request->param('pass'));
        exitJson($ret);
    }
    //编辑及修改权限
    public function edit()
    {
        if ($this->request->isPost()) {
            exitJson(UserService::editPost($this->param));
        }else{
            exitJson(UserService::editGet($this->param));
        }
    }
    public function editBg()
    {
        if ($this->request->isPost()) {
            exitJson(UserService::editBg($this->param));
        }
    }
    //退出登陆
    public function logout(){
        session('user', null);
        returnJson();
    }
    //获取配置缓存到前端
    public function config(){
        if(UID){
            exitJson(UserService::config(UID));
        }
        returnJson(500);
    }
    public function getUserByName(){
        $list=UserModel::getUserByName(['name'=>$this->param['keyword']]);
        $data=[];
        if(empty($list)){
            returnJson(0,'',$data);
        }
        foreach ($list as $v){
            $data[]=["name"=>$v['nike'],"value"=>$v['id'],"selected"=>"","disabled"=>""];
        }
        returnJson(0,'',$data);
    }
    public function getUserByCnName(){
        $list=UserModel::getUserByName(['name'=>$this->param['keyword']]);
        $data=[];
        if(empty($list)){
            returnJson(0,'',$data);
        }
        foreach ($list as $v){
            $data[]=["name"=>$v['nike'],"value"=>$v['nike'],"selected"=>"","disabled"=>""];
        }
        returnJson(0,'',$data);
    }
}
